package com.it.exprezoorecords.db;

import java.text.MessageFormat;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper{

	public static final String DATABASE_NAME = "track.db";
	public static final int SCHEMA_VERSION = 1;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, SCHEMA_VERSION);
		// TODO Auto-generated constructor stub
	}
	
	

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "CREATE TABLE {0} ({1} INTEGER PRIMARY KEY AUTOINCREMENT," + 
		" {2} TEXT NOT NULL,{3} TEXT NOT NULL);";
		db.execSQL(MessageFormat.format(sql, TrackTable.TABLE_NAME, TrackTable._ID,
				TrackTable.TITLE, TrackTable.AUTHOR));
 
		inserisciTraccia(db, "Traccia1", "A1");
		inserisciTraccia(db, "Traccia2", "A2");
		//...
		inserisciTraccia(db, "Traccia3", "A3");	
		inserisciTraccia(db, "Traccia4", "A4");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub	
	}
	
	private void inserisciTraccia(SQLiteDatabase db, String title, String author) {
		ContentValues v = new ContentValues();
		v.put(TrackTable.TITLE, title);
		v.put(TrackTable.AUTHOR, author);
		db.insert(TrackTable.TABLE_NAME, null, v);		
	}
	
	public Cursor getTrack() {		
		return (getReadableDatabase().query(TrackTable.TABLE_NAME, TrackTable.COLUMNS, null, null, null, null, TrackTable.TITLE));		
	}
	
	
	public Cursor getAllTracks()
	{
		String query = "select * from " + TrackTable.TABLE_NAME;
		return getReadableDatabase().rawQuery(query, null);
	}

}
